package main

import (
	"html/template"
	"net/http"
)

type User struct {
	Name   string
	Gender string
	Age    int64
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	//解析模版
	t, err := template.ParseFiles("./index.tmpl")
	if err != nil {
		panic(err)
	}
	//渲染模版
	u1 := User{
		Name:   "张三",
		Gender: "男",
		Age:    18,
	}
	m1 := map[string]interface{}{
		"name":   "李四",
		"gender": "女",
		"age":    20,
	}
	hobbylist := []string{
		"篮球",
		"足球",
		"乒乓球",
	}
	//name := "张三"
	err = t.Execute(w, map[string]interface{}{
		"u1":    u1,
		"m1":    m1,
		"hobby": hobbylist,
	})
	if err != nil {
		panic(err)
	}
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}
